package com.rmfyzxfw.caseprocess.controller;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rmfyzxfw.caseprocess.boot.result.page.Pagination;
import com.rmfyzxfw.caseprocess.enums.EnableStatusEnum;
import com.rmfyzxfw.caseprocess.enums.True1False0;
import com.rmfyzxfw.caseprocess.model.cmd.DataNodeAbilityCmd;
import com.rmfyzxfw.caseprocess.model.cmd.DataNodeAbilityEditCmd;
import com.rmfyzxfw.caseprocess.model.cmd.DataNodeAbilityManageCmd;
import com.rmfyzxfw.caseprocess.model.converter.DataNodeAbilityConverter;
import com.rmfyzxfw.caseprocess.model.param.DataNodeAbilityListParam;
import com.rmfyzxfw.caseprocess.model.po.DataNodeAbility;
import com.rmfyzxfw.caseprocess.model.vo.DataNodeAbilityListVO;
import com.rmfyzxfw.caseprocess.service.DataNodeAbilityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * @description:
 * @Author: GYW  742926306@qq.com
 * @Date: 2024/9/10 10:35
 */
@RestController
@RequestMapping("api/v3/dataNodeAbility")
@RequiredArgsConstructor
@Api(tags = "数据节点能力")
public class DataNodeAbilityController {

    private final DataNodeAbilityService dataNodeAbilityService;
    private final DataNodeAbilityConverter dataNodeAbilityConverter;

    @PostMapping("add")
    @ApiOperation(value = "新增节点能力")
    public void add(@RequestBody DataNodeAbilityCmd cmd){
        DataNodeAbility dataNodeAbility = dataNodeAbilityConverter.cmd2do(cmd);
        dataNodeAbility.setDataNodeAbilityId(IdUtil.simpleUUID());
        dataNodeAbility.setStatus(EnableStatusEnum.DISABLE.getCode());
        dataNodeAbilityService.save(dataNodeAbility);
    }

    @PostMapping("edit")
    @ApiOperation(value = "编辑节点能力")
    public void edit(@RequestBody DataNodeAbilityEditCmd cmd){
        DataNodeAbility dataNodeAbility = dataNodeAbilityConverter.cmd2do(cmd);
        dataNodeAbilityService.update(dataNodeAbility, Wrappers.<DataNodeAbility>lambdaQuery().eq(DataNodeAbility::getDataNodeAbilityId, cmd.getDataNodeAbilityId()));
    }

    @PostMapping("manage")
    @ApiOperation(value = "管理节点能力 删除 启用/禁用")
    public void manage(@RequestBody DataNodeAbilityManageCmd cmd){
        if(True1False0.TRUE.getCode().equals(cmd.getRemove())){
            dataNodeAbilityService.remove(Wrappers.<DataNodeAbility>lambdaQuery().eq(DataNodeAbility::getDataNodeAbilityId, cmd.getDataNodeAbilityId()));
            return;
        }

        DataNodeAbility dataNodeAbility = dataNodeAbilityConverter.cmd2do(cmd);
        dataNodeAbilityService.update(dataNodeAbility, Wrappers.<DataNodeAbility>lambdaQuery().eq(DataNodeAbility::getDataNodeAbilityId, cmd.getDataNodeAbilityId()));

    }

    @GetMapping("page")
    @ApiOperation(value = "节点能力列表")
    public Pagination<DataNodeAbilityListVO> page(DataNodeAbilityListParam param){
        Page<DataNodeAbility> page = new Page<>(param.getPageNum(), param.getPageSize());
        Page<DataNodeAbility> nodeAbilityPage = dataNodeAbilityService.page(page, Wrappers.<DataNodeAbility>lambdaQuery().eq(DataNodeAbility::getDataNodeId, param.getDataNodeId())
                .like(StringUtils.isNotBlank(param.getButtonName()), DataNodeAbility::getButtonName, param.getButtonName())
                .orderByDesc(DataNodeAbility::getId));

        return Pagination.build(nodeAbilityPage, dataNodeAbilityConverter::dos2vos);
    }
}